# coding:utf-8
import requests
from lxml import etree

url = "https://www.bilibili.com/video/BV16q4y1h7v7"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36"
}
response = requests.get(url=url, headers=headers)
# with open("bilibili_qian.html", "w", encoding="utf-8") as fp:
#     fp.write(response.text)

BV = "bvid=" + url[-12:]
bv_list = response.text.split(":{")
cab_cpl1 = None
cab_cpl2 = None
cid = None
aid = None
bvid = None
for i in bv_list:
    if BV in i:
        cab_cpl1 = i.split("\"")
        break
for j in cab_cpl1:
    if BV in j:
        cab_cpl2 = j.split("&")
        break
for x in cab_cpl2:
    if "cid" in x:
        cid = x
    elif "aid" in x:
        aid = x
    elif "bvid" in x:
        bvid = x
# print(cid,aid,bvid)
if aid != None and bvid != None and cid != None:
    iframe = '<iframe src="//player.bilibili.com/player.html?{}&{}&{}&page=1&high_quality=1&danmaku=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>'.format(
        aid, bvid, cid)
print(iframe)
